﻿using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace Open.Infrastructure.Http
{
    public class HttpClientBase
    {
        public HttpClient Client { get; }

        public HttpClientBase(HttpClient client)
        {
            client.BaseAddress = new Uri("http://192.168.22.45:9744/");
            // GitHub API versioning
            client.DefaultRequestHeaders.Add("Accept", "application/dicom+json");
            // GitHub requires a user-agent
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");
            Client = client;
        }


        #region 泛型T
        public async Task<string> GetStringAsync(string uri)
        {
            var response = await Client.GetAsync(uri);
            response.EnsureSuccessStatusCode();
            var result = response.Content.ReadAsStringAsync().Result;
            return result;
        }

        public async Task<T> PostAsync<T>(string uri, HttpContent content)
        {
            var response = await Client.PatchAsync(uri, content);
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsAsync<T>();
            return result;
        }

        public async Task<T> DeleteAsync<T>(string uri)
        {
            var response = await Client.DeleteAsync(uri);
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsAsync<T>();
            return result;
        }

        public async Task<T> PutAsync<T>(string uri, HttpContent content)
        {
            var response = await Client.PutAsync(uri, content);
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsAsync<T>();
            return result;
        }
        #endregion
    }
}
